package com.rainng.coursesystem.service.student;

import com.rainng.coursesystem.dao.mapper.CourseMapper;
import com.rainng.coursesystem.dao.mapper.ExamMapper;
import com.rainng.coursesystem.dao.mapper.StudentCourseMapper;
import com.rainng.coursesystem.dao.mapper.TeacherMapper;
import com.rainng.coursesystem.manager.student.ExamManager;
import com.rainng.coursesystem.model.entity.ExamEntity;
import com.rainng.coursesystem.model.vo.response.ResultVO;
import com.rainng.coursesystem.model.vo.response.table.StudentExamItemVO;
import com.rainng.coursesystem.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExamService extends BaseService {
    private final ExamManager manager;
    @Autowired
    private ExamMapper mapper;
    @Autowired
    private StudentCourseMapper studentCourseMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    public ExamService(ExamManager manager) {
        this.manager = manager;
    }
    @Transactional
    public ResultVO list() {
        List<Integer> courseList = studentCourseMapper.getCourseListByStudentId(getUserId());
        List<ExamEntity> examEntities = mapper.getByCourseIdList(courseList);
        List<StudentExamItemVO> studentExamItemVOS=new ArrayList<>();
        if(examEntities!=null&&examEntities.size()!=0){
            for (ExamEntity examEntity : examEntities) {
                StudentExamItemVO studentExamItemVO = new StudentExamItemVO();
                studentExamItemVO.setExamLocation(examEntity.getLocation());
                studentExamItemVO.setExamDate(examEntity.getBeginTime());
                studentExamItemVO.setCourseName(courseMapper.selectById(examEntity.getCourseId()).getName());
                studentExamItemVO.setTeacherName(teacherMapper.selectById(courseMapper.selectById(examEntity.getCourseId()).getTeacherId()).getName());
                studentExamItemVOS.add(studentExamItemVO);
            }

        }
        return result(studentExamItemVOS);
    }
}
